/**
 * 已基本过时, 将由dxjango.setting模块代替.
 */

var FG;
if (!FG)
    FG = {};

FG.Settinger = function() {
    var this_ = this;
    
    this.moduleInfo = {
        name: 'settinger',
        zhName: '设置管理器',
        desc: '设置管理器',
        exportList: [
            'getSetting', 'getSysSetting', 'getUserSetting', 'getUserStatus',
            'loadSetting', 'loadSysSetting', 'loadUserSetting', 'loadUserStatus',
            '_setSetting',
            'saveUserSetting', 
            'updateSetting',
            'registerSettingCallback'
        ]
    };
    
    this._callbacks = {
        'sys': {},
        'user': {},
        'userStatus': {}
    };
    
    this.settings = {
        'sys': {},
        'user': {},
        'userStatus': {}
    };
    
    this.getSetting = function(category, key, defaultValue) {
        var cur = this.settings[category];
        if (typeof key === 'undefined') {
            return cur;
        }
        var tokens = key.split('.');
        for (var i=0; i < tokens.length; i+=1) {
            if (typeof cur === 'undefined') {
                break;
            }
            cur = cur[tokens[i]];
        }
        if (typeof cur === 'undefined') {
            return defaultValue;
        } else {
            return cur;
        }
    };
    
    this.getSysSetting = function(key, defaultValue) { return this.getSetting('sys', key, defaultValue); };
    this.getUserSetting = function(key, defaultValue) { return this.getSetting('user', key, defaultValue); };
    this.getUserStatus = function(key, defaultValue) { return this.getSetting('userStatus', key, defaultValue); };

    this._setSetting = function(category, key, value) {
        var cur = this.settings[category];
        var tokens = (_.isString(key)) ? key.split('.') : key;
        for (var i=0; i < tokens.length; i+=1) {
            var tk = tokens[i];
            if (i === tokens.length-1) {
                cur[tk] = value;
                break;
            }
            
            if (!_.has(cur, tk)) {
                cur[tk] = {}
            }
            cur = cur[tk];
        }
    };

    this.loadSetting = function(category) {
        var url;
        switch (category) {
            case 'sys':
                url = '/setting/system/';
                break;
            case 'user':
                url = '/setting/user/';
                break;
            case 'userStatus':
                url = '/setting/user_status/';
                break;
        }
        $.ajax({
            dataType: "json",
            url: url,
            success: function(data) {
                this_.updateSetting(category, data);
            }
        })
    };
    
    // shortcuts
    this.loadSysSetting = function() { this.loadSetting('sys'); };
    this.loadUserSetting = function() { this.loadSetting('user'); };
    this.loadUserStatus = function() { this.loadSetting('userStatus'); };
    
    this.updateSetting = function(category, data, opts) {
        var old = this.settings[category];
        var diffs = DeepDiff.diff(old, data);
        _.each(diffs, function(d) {
            if (d.kind === 'D' && !!opts && !!opts.ignoreDeleted) {
                return;
            }

            var key = d.path.join('.');
            if (d.kind === 'N') {
                DeepDiff.applyChange(old, data, d);
            } else if (d.kind === 'E') {
                DeepDiff.applyChange(old, data, d);
            } else if (d.kind === 'D') {
                DeepDiff.applyChange(old, data, d);
            } else if (d.kind === 'A') {
                DeepDiff.applyChange(old, data, d);
            }
            // callback
            var callbackGroup = this_._callbacks[category];
            if (_.has(callbackGroup, key)) {
                _.each(callbackGroup[key], function(callback) {
                    callback(this_.getSetting(category, key));
                })
            }
        })
    };
    
    this.saveUserSetting = function(data) {
        var url = '/setting/user/';
        $.post(url, JSON.stringify(data), function() {
            this_.updateSetting('user', data, {ignoreDeleted: true});
        });
    };
    
    this.sendActivity = function(data) {
        var url = '/setting/activity/';
        $.post(url, JSON.stringify(data));
    };
    
    this.registerSettingCallback = function(category, key, callback) {
        var callbackGroup = this._callbacks[category];
        if (!_.has(callbackGroup, key)) {
            callbackGroup[key] = [];
        }
        callbackGroup[key].push(callback);
    }
};

